{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# A Cantera Simulation with Reaction Sensitivity: Comparison with Native RMG Sensitivity Analysis and CHEMKIN Sensitivity Analysis\n",
    "\n",
    "\n",
    "Note that this requires Cantera with SUNDIALS installed for sensitivity support.  If you are using Anaconda,\n",
    "Cantera version >= 2.3.0 is required"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import cantera\n",
    "print(cantera.__version__)  # Check Cantera version"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import shutil\n",
    "\n",
    "from IPython.display import display, Image\n",
    "\n",
    "from rmgpy.chemkin import load_chemkin_file\n",
    "from rmgpy.species import Species\n",
    "from rmgpy.tools.canteraModel import Cantera, get_rmg_species_from_user_species\n",
    "from rmgpy.tools.plot import SimulationPlot, ReactionSensitivityPlot, parse_csv_data\n",
    "from rmgpy.tools.simulate import run_simulation"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Load the species and reaction from the RMG-generated chemkin file `chem_annotated.inp` and `species_dictionary.txt` file found in your `chemkin` folder after running a job."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "species_list, reaction_list = load_chemkin_file('./data/ethane_model/chem_annotated.inp',\n",
    "                                                './data/ethane_model/species_dictionary.txt',\n",
    "                                                './data/ethane_model/tran.dat')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Set the reaction conditions"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Find the species: ethane and methane\n",
    "user_ethane = Species().from_smiles('CC')\n",
    "user_methane = Species().from_smiles('C')\n",
    "species_dict = get_rmg_species_from_user_species([user_ethane, user_methane], species_list)\n",
    "ethane = species_dict[user_ethane]\n",
    "methane = species_dict[user_methane]\n",
    "sensitive_species = [ethane, methane]\n",
    "\n",
    "#reactor_type_list = ['IdealGasReactor']\n",
    "reactor_type_list = ['IdealGasConstPressureTemperatureReactor']\n",
    "mol_frac_list = [{ethane: 1}]\n",
    "Tlist = ([1300], 'K')\n",
    "Plist = ([1], 'bar')\n",
    "reaction_time_list = ([0.5], 'ms')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Create cantera object, loading in the species and reactions\n",
    "job = Cantera(species_list=species_list, reaction_list=reaction_list, output_directory='temp', sensitive_species=sensitive_species)\n",
    "# The cantera file must be created from an associated chemkin file\n",
    "\n",
    "# We can either load the Model from the initialized set of rmg species and reactions\n",
    "job.load_model()\n",
    "\n",
    "# Or load it from a chemkin file by uncommenting the following line:\n",
    "#job.load_chemkin_model('data/ethane_model/chem_annotated.inp',transport_file='data/ethane_model/tran.dat')\n",
    "\n",
    "# Generate the conditions based on the settings we declared earlier\n",
    "job.generate_conditions(reactor_type_list, reaction_time_list, mol_frac_list, Tlist, Plist)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Simulate and plot\n",
    "alldata = job.simulate()\n",
    "job.plot(alldata)\n",
    "\n",
    "# Show the plots in the ipython notebook\n",
    "for i, condition in enumerate(job.conditions):\n",
    "    print('Cantera Simulation: Condition {0} Species Mole Fractions'.format(i+1))\n",
    "    display(Image(filename=\"temp/{0}_mole_fractions.png\".format(i+1)))\n",
    "    \n",
    "    print('Cantera Simulation: Condition {0} Ethane Reaction Sensitivity'.format(i+1))\n",
    "    display(Image(filename=\"temp/{0}_ethane(1)_sensitivity.png\".format(i+1)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "# Copy example input file to temp folder\n",
    "shutil.copy('./data/ethane_model/input.py', './temp')\n",
    "\n",
    "# We can run the same simulation using RMG's native solver\n",
    "run_simulation(\n",
    "    './temp/input.py',\n",
    "    './data/ethane_model/chem_annotated.inp',\n",
    "    './data/ethane_model/species_dictionary.txt',\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print('RMG Native Simulation: Species Mole Fractions')\n",
    "display(Image(filename=\"./temp/solver/simulation_1_27.png\"))\n",
    "\n",
    "print('RMG Native Simulation: Ethane Reaction Sensitivity')\n",
    "display(Image(filename=\"./temp/solver/sensitivity_1_SPC_1_reactions.png\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Let's also compare against the same simulation and sensitivity analysis that was conducted in CHEMKIN\n",
    "# and saved as a .csv file\n",
    "time, data_list = parse_csv_data('./data/ethane_model/chemkin_mole_fractions.csv')\n",
    "SimulationPlot(x_var=time, y_var=data_list, num_species=10).plot('./temp/chemkin_mole_fractions.png')\n",
    "print('CHEMKIN Simulation: Species Mole Fractions')\n",
    "display(Image(filename=\"./temp/chemkin_mole_fractions.png\"))\n",
    "\n",
    "time, data_list = parse_csv_data('./data/ethane_model/chemkin_sensitivity_ethane.csv')\n",
    "ReactionSensitivityPlot(x_var=time, y_var=data_list, num_reactions=10).barplot('./temp/chemkin_sensitivity_ethane.png')\n",
    "print('CHEMKIN Simulation: Ethane Reaction Sensitivity')\n",
    "display(Image(filename=\"./temp/chemkin_sensitivity_ethane.png\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python [conda env:rmg_env]",
   "language": "python",
   "name": "conda-env-rmg_env-py"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
